package controllers.warehouse;

import controllers.BaseController;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import helper.GlobalConfig;
import me.chanjar.weixin.common.util.StringUtils;
import models.AdminSupplier;
import models.constants.DeletedStatus;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by youliangcheng on 17/7/10.
 */
@With(BaseController.class)
public class AdminSupplierCtrl extends Controller {

    //列表页
    public static void LoadList(Integer pageNumber,
                                Long id ,
                                String code ,
                                String name ,
                                String phone){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();

        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%"+code+"%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%"+name+"%");

        if (StringUtils.isNotBlank(phone))
            searchMap.put("phone", "%"+phone+"%");


        JPAExtPaginator<AdminSupplier> resultPage = AdminSupplier.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, code, name, phone );

    }

    //删除
    public static void deleteSupplier(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> resultMap = new HashMap<>();
        AdminSupplier adminSupplier = AdminSupplier.findBySupplierId(id);
        if(adminSupplier == null){
            resultMap.put("success",false);
            resultMap.put("msg","数据不存在,可能已经被删除!");
            renderJSON(resultMap);

        }
        adminSupplier.deleted = DeletedStatus.DELETED ;
        Secure.setBaseInfo(adminSupplier);
        adminSupplier.save();

        resultMap.put("success",true);
        resultMap.put("pageNumber",pageNumber);
        renderJSON(resultMap);
    }

    //进入详情页
    public static void loadDetail(long id, Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        AdminSupplier adminSupplier = new AdminSupplier();
        if(id > 0)
            adminSupplier = AdminSupplier.findBySupplierId(id);

        render(adminSupplier , pageNumber) ;
    }

    //保存
    public static void saveDetail(AdminSupplier adminSupplier , Integer pageNumber){

        Secure.setBaseInfo(adminSupplier);
        if(adminSupplier.id == null){
            adminSupplier.loadKey();
            adminSupplier.deleted = DeletedStatus.UN_DELETED ;
            adminSupplier.save();
        }else {
            adminSupplier.update();
        }
        flash.put("msg","保存成功!");
        redirect("/warehouse/AdminSupplierCtrl/LoadList/"+pageNumber);

    }

    @SkipAuth
    public static void popAdminSuppliers(Integer pageNumber,
                                         String code ,
                                         String name ,
                                         String phone){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%"+code+"%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%"+name+"%");

        if (StringUtils.isNotBlank(phone))
            searchMap.put("phone", "%"+phone+"%");


        JPAExtPaginator<AdminSupplier> resultPage = AdminSupplier.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber , code, name, phone);

    }

}
